package com.song.ejf.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository("oracleDao")
public class OracleDaoImpl extends HibernateDaoSupport implements OracleDao {
	Log log = LogFactory.getLog(OracleDaoImpl.class);
	@Autowired
	@Qualifier("sessionFactory")
	public void setSessionFactory1(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public Map<String, Object> testProcedure(final String username,final String password) {
		final Map<String, Object> resultMap = new HashMap<String, Object>();
		getSession().doWork(new Work() {
			@Override
			public void execute(Connection conn) throws SQLException {
				CallableStatement st = conn.prepareCall("{call ejf_user_pak.test_proc(?,?,?,?)}");
				st.setString(1, username);
				st.setString(2, password);
				st.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
				st.registerOutParameter(4, oracle.jdbc.OracleTypes.VARCHAR);
				st.execute();
				ResultSet rs = null;
				rs = (ResultSet)st.getObject(3);
				while(rs.next()){
					System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>row:" + rs.getString("un"));
					System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>row:" + rs.getString("uuid"));
					System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>row:" + rs.getString("pw"));
				}
				String count = st.getString(4);
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>:" + count);
				if(rs != null){
					rs.close();
				}
				if(conn != null){
					conn.close();
				}
			}
		});
		return resultMap;
	}
}
